import pytest
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from BasePage import *
import time
from selenium.webdriver import ChromeOptions, ActionChains

# 这里文件名必须是conftest
# file_name = os.path.join(ELEMENT_DIR,get_current_package_name(os.path.abspath(__file__)))
file_name = os.path.join(ELEMENT_DIR,"login_page.yml")


@pytest.fixture(scope="session")
class Driver(BasePage):

    def __init__(self):
        # 创建Chrome浏览器的Webdriver对象
        # options.add_argument("--headless")  # 无界面模式
        options = webdriver.ChromeOptions()
        # 使用ChromeDriverManager来自动下载和安装Chrome驱动程序
        self.driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()),options=options)

    def login_system(self,url="http://192.168.0.57:8666/#/login"):
        # 输入系统地址
        self.driver.get(url)
        # 窗口最大化
        self.driver.maximize_window()
        # 系统全局等待时间
        self.driver.implicitly_wait(20)
        # 登录操作
        # 输入用户名
        self.input_text(file_name,"username_input","18323585901")
        # 输入密码
        self.input_text(file_name,"password_input","Zwq941106")
        # 移动滑块验证
        # 事件参数对象
        actionChains = ActionChains(self.driver)
        # 捕捉滑块元素
        get_locator = get_ele_locator(file_name,"move_slider")
        slide_btn = self.find_element(get_locator)
        # 观察网站滑块移动的长度和位置
        actionChains.drag_and_drop_by_offset(slide_btn, 330, 0).perform()
        #点击登录
        self.click_element(file_name,"login_submit")
        sleep(10)



if __name__ == '__main__':
    driver = Driver()
    driver.login_system()